﻿using Pb;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class NNGameOver : MonoBehaviour
{
    EginDN dn = EginDN.Instance;
    EginUser user = EginUser.Instance;
    public List<GameObject> Score;
    public List<GameObject> ScoreFather; //字体位置
    public List<UISprite> jiajian;
    public List<GameOverUserInfo> gameOverUserInfo;
    //blue 4CBFD9FF
    //Red  d13411
    public GameObject Lose;
    public GameObject Win;
    private int OwnScore;
    private bool isHaveOwnScore = false;
    public bool IsOver;



    private void Start()
    {


        for (int i = 0; i < gameOverUserInfo.Count; i++)
        {
            if (dn.seatId == gameOverUserInfo[i].SeatId)
            {
                OwnScore = gameOverUserInfo[i].RoundScore;
                isHaveOwnScore = true;
                break;
            }
        }
        if (isHaveOwnScore)
        {
            if (OwnScore > 0)
            {
                Win.SetActive(true);
            }
            else
            {
                Lose.SetActive(true);
            }
        }

        for (int i = 0; i < gameOverUserInfo.Count; i++)
        {
            //获得 在场玩家座位id
            int tempIndex = dn.returnOtherSeatIndex(gameOverUserInfo[i].SeatId);
            #region 计算分数
            int tempScore = gameOverUserInfo[i].RoundScore;
            string tempStr = user.ReturnRelCoin(Mathf.Abs(gameOverUserInfo[i].RoundScore));
            //Debug.Log(tempStr);
            float x = Score[tempIndex].transform.localPosition.x;
            float y = Score[tempIndex].transform.localPosition.y;
            if (tempScore > 0)
            {
                jiajian[tempIndex].gameObject.SetActive(true);
                jiajian[tempIndex].spriteName = "plus_" + "jia";
                int len = tempStr.Length;
                for (int j = 0; j < len; j++)
                {
                    GameObject obj = (GameObject)Instantiate(Score[tempIndex]);
                    obj.SetActive(true);
                    obj.transform.parent = ScoreFather[tempIndex].transform;
                    obj.transform.localScale = new Vector3(1, 1, 1);
                    string str = tempStr.Substring(0, 1);
                    if (j == 0)
                    {
                        obj.transform.localPosition = new Vector3(x, y, 0);
                    }
                    else
                    {
                        if (str == ".")
                        {
                            x += 26;

                        }
                        else if (str == "w")
                        {
                            x += 50;
                        }
                        else
                        {
                            x += 40;
                        }
                        obj.transform.localPosition = new Vector3(x, y, 0);
                    }
                    tempStr = tempStr.Remove(0, 1);
                    obj.GetComponent<UISprite>().spriteName = "plus_" + str;
                }
            }
            else
            {
                jiajian[tempIndex].gameObject.SetActive(true);
                jiajian[tempIndex].spriteName = "less_" + "jian";
                int len = tempStr.Length;
                for (int j = 0; j < len; j++)
                {
                    GameObject obj = (GameObject)Instantiate(Score[tempIndex]);
                    obj.SetActive(true);
                    obj.transform.parent = ScoreFather[tempIndex].transform;
                    obj.transform.localScale = new Vector3(1, 1, 1);
                    string str = tempStr.Substring(0, 1);
                    if (j == 0)
                    {
                        obj.transform.localPosition = new Vector3(x, y, 0);
                    }
                    else
                    {
                        if (str == ".")
                        {
                            x += 26;
                        }
                        else if (str == "w")
                        {
                            x += 50;
                        }
                        else
                        {
                            x += 40;
                        }
                        obj.transform.localPosition = new Vector3(x, y, 0);
                    }
                    tempStr = tempStr.Remove(0, 1);
                    obj.GetComponent<UISprite>().spriteName = "less_" + str;
                }
            }
            #endregion
            
            //GameObject obj = (GameObject)Instantiate(dapaistart);
        }
        //for (int j = 0; j < ScoreFather.Count; j++)
        //{
        //    ScoreFather[j].transform.localPosition = GameObject.Find("UI Root/FixUI/Users/User" + (j + 1)).transform.localPosition;
        //}
        StartCoroutine(GameOver());
    }

    IEnumerator GameOver()
    {
        EginUser user = EginUser.Instance;
        yield return new WaitForSeconds(3.0f);
        if (IsOver)
        {
            GameObject NNSettlement = NGUITools.AddChild(GameObject.Find("UI Root"), (GameObject)(Resources.Load(GameDNPrafabPath.NNSettlement)));
            NNSettlement.GetComponent<NNSettlement>().LoadInfo(gameOverUserInfo);
        }
        else
        {
            Destroy(GameObject.Find("Cards"));
            NNGame nngame = GameObject.Find("Camera").GetComponent<NNGame>();
            nngame.UserInit();
            for (int i = 0; i < gameOverUserInfo.Count; i++)
            {
                if (gameOverUserInfo[i].SeatStatus == SEAT_STATUS.Leave && user.ControlScene == 2)
                {
                    for (int j = 0; j < user.gameUserInfoList.Count; j++)
                    {
                        user.gameUserInfoList[j].IsWitness = false;
                        if (gameOverUserInfo[i].SeatId == user.gameUserInfoList[j].SeatId)
                        {
                            user.gameUserInfoList.RemoveAt(j);
                            int tempIndex = dn.returnOtherSeatIndex(gameOverUserInfo[i].SeatId);
                            nngame.Users[tempIndex].avatarSprit.gameObject.SetActive(false);
                            nngame.Users[tempIndex].WaitLabel.gameObject.SetActive(true);
                            nngame.Users[tempIndex].UserNameLabel.gameObject.SetActive(false);
                            nngame.Users[tempIndex].GoldLabel.gameObject.SetActive(false);
                            if (user.tempGameUserInfoList.Count > 0)
                            {
                                for (int y = 0; y < user.tempGameUserInfoList.Count; y++)
                                {
                                    int index = dn.returnOtherSeatIndex(user.tempGameUserInfoList[y].SeatId);
                                    nngame.Users[index].SeatID = user.tempGameUserInfoList[y].SeatId;
                                    nngame.Users[index].avatarSprit.gameObject.SetActive(true);
                                    nngame.Users[index].avatarSprit.spriteName = user.tempGameUserInfoList[y].AvatarId.ToString();
                                    nngame.Users[index].avatarSprit.gameObject.GetComponent<UIButton>().normalSprite = user.tempGameUserInfoList[y].AvatarId.ToString();
                                    nngame.Users[index].WaitLabel.gameObject.SetActive(false);
                                    nngame.Users[index].UserNameLabel.gameObject.SetActive(true);
                                    nngame.Users[index].UserNameLabel.text = user.tempGameUserInfoList[y].Nickname;
                                    nngame.Users[index].GoldLabel.gameObject.SetActive(true);
                                    if (user.ControlScene == 1)
                                    {
                                        string str = user.ReturnRelCoin(user.tempGameUserInfoList[y].Score);
                                        nngame.Users[index].GoldLabel.text = str;
                                    }
                                    else
                                    {
                                        string str = user.ReturnRelCoin(user.tempGameUserInfoList[y].BagCoinCount);
                                        nngame.Users[index].GoldLabel.text = str;
                                    }
                                    user.gameUserInfoList.Add(user.tempGameUserInfoList[y]);
                                }
                            }
                            user.tempGameUserInfoList.Clear();
                            break;

                        }
                    }
                }
            }
            Destroy(gameObject);

        }



    }
}
